/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.geometry;

import org.openaecfm.model.geometry.RectangularTrimmedSurface;
import org.openaecfm.model.geometry.Surface;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class RectangularTrimmedSurfaceIO extends
		AbstractEntityInstanceInputImpl<RectangularTrimmedSurface> implements
		IEntityInstanceOutput<RectangularTrimmedSurface> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(RectangularTrimmedSurface pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCRECTANGULARTRIMMEDSURFACE", appDataManager.getName(pojo));

		// Set BasisSurface
		EntityInstanceNameParameter basissurface = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getBasisSurface()));
		ei.addParameter(basissurface);

		// Set U1
		RealParameter u1 = new RealParameter(pojo.getU1());
		ei.addParameter(u1);

		// Set V1
		RealParameter v1 = new RealParameter(pojo.getV1());
		ei.addParameter(v1);

		// Set U2
		RealParameter u2 = new RealParameter(pojo.getU2());
		ei.addParameter(u2);

		// Set V2
		RealParameter v2 = new RealParameter(pojo.getV2());
		ei.addParameter(v2);

		// Set Usense
		EnumerationParameter usense = new EnumerationParameter(pojo.getUsense());
		ei.addParameter(usense);

		// Set Vsense
		EnumerationParameter vsense = new EnumerationParameter(pojo.getVsense());
		ei.addParameter(vsense);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected RectangularTrimmedSurface doCreateObject() {
		return new RectangularTrimmedSurface();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			RectangularTrimmedSurface pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set BasisSurface
		EntityInstanceNameParameter basis = (EntityInstanceNameParameter) sei
				.getParameter(0);
		pojo.setBasisSurface((Surface) appDataManager.getObject(basis
				.toInstanceName()));
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			RectangularTrimmedSurface pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set U1
		RealParameter u1 = (RealParameter) sei.getParameter(1);
		pojo.setU1(u1.toDouble());

		// Set V1
		RealParameter v1 = (RealParameter) sei.getParameter(2);
		pojo.setV1(v1.toDouble());

		// Set U2
		RealParameter u2 = (RealParameter) sei.getParameter(3);
		pojo.setU2(u2.toDouble());

		// Set V2
		RealParameter v2 = (RealParameter) sei.getParameter(4);
		pojo.setV2(v2.toDouble());

		// Set Usense
		EnumerationParameter u = (EnumerationParameter) sei.getParameter(5);
		pojo.setUsense((org.openaecfm.model.measure.Boolean.valueOf(u
				.toEnumString())));

		// Set Vsense
		EnumerationParameter v = (EnumerationParameter) sei.getParameter(6);
		pojo.setVsense((org.openaecfm.model.measure.Boolean.valueOf(v
				.toEnumString())));
	}
}
